package com.resource.utils;

import cn.hutool.core.date.DateUtil;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;

/**
 * 泛微查询条件拼接
 *
 * @Author: xianggaoxiong
 * @Date: 2020-09-23 11:39
 **/
public class SearchUtils {
    private static final String PAGE = "page";
    private static final String LIMIT = "limit";
    private static final String UID = "serialVersionUID";

    public static Map<String, List<String>> getQuery(Object obj) throws Exception {
        // 获取实体类的所有属性，返回Field数组
        Field[] field = obj.getClass().getDeclaredFields();
        Map<String, List<String>> map = new HashMap<>(field.length);
        // 遍历所有属性
        for (int j = 0; j < field.length; j++) {
            List<String> search = new ArrayList<>();
            // 获取属性名称
            String originName = field[j].getName();
            if (originName.equals(PAGE) || originName.equals(LIMIT) || originName.equals(UID)|| originName.equals("userId")) {
                continue;
            }
            // 将属性的首字符大写，方便构造get，set方法
            String fname = originName.substring(0, 1).toUpperCase() + originName.substring(1);
            // 获取属性的类型
            String ftype = field[j].getGenericType().toString();
            // 如果ftype是类类型，则前面包含"class "，后面跟类名
            if ("class java.lang.String".equals(ftype)) {
                Method m = obj.getClass().getMethod("get" + fname);
                // 利用反射原理，调用getter方法获取属性值
                String value = (String) m.invoke(obj);
                if (value != null) {
                    search.add(value);
                    search.add("=");
                    map.put(originName, search);
                }
            }
            if ("class java.lang.Integer".equals(ftype)) {
                Method m = obj.getClass().getMethod("get" + fname);
                Integer value = (Integer) m.invoke(obj);
                if (value != null) {
                    search.add(String.valueOf(value));
                    search.add("=");
                    map.put(originName, search);
                }
            }
            if (("class java.lang.Long").equals(ftype)) {
                Method m = obj.getClass().getMethod("get" + fname);
                Long value = (Long) m.invoke(obj);
                if (value != null) {
                    search.add(String.valueOf(value));
                    search.add("=");
                    map.put(originName, search);
                }
            }
            if ("class java.lang.Double".equals(ftype)) {
                Method m = obj.getClass().getMethod("get" + fname);
                Double value = (Double) m.invoke(obj);
                if (value != null) {
                    search.add(String.valueOf(value));
                    search.add("=");
                    map.put(originName, search);
                }
            }
            if ("class java.util.Date".equals(ftype)) {
                Method m = obj.getClass().getMethod("get" + fname);
                Date value = (Date) m.invoke(obj);
                if (value != null) {
                    search.add(DateUtil.formatDateTime(value));
                    search.add("=");
                    map.put(originName, search);
                }
            }
        }
        return map;
    }
}
